home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / pctuto.zip / DISK3.EXE / lha / CHAP10-1.DOC < prev    next >
Text File  |  1990-06-24  |  31KB  |  687 lines

  1.  
  2.  
  3.  
  4.                                                                             78
  5.  
  6.  
  7.                                    CHAPTER 10 - TEMPLATES
  8.  
  9.  
  10.              Do you remember when you were younger and you needed to look up a
  11.              word in the dictionary? It would define the word in terms of a
  12.              second word which you didn't know so you would look that up too.
  13.              Most likely that second word was either defined in terms of a
  14.              third word you didn't know or it referred you back to the first
  15.              word.
  16.  
  17.              This chapter is something like that. The items in the template
  18.              file are interdependent. If you're lucky, everything will be
  19.              clear by the time you have finished the chapter. If not, you'll
  20.              have to reread it.
  21.  
  22.              There are four different things which operate on the assembler
  23.              instructions which you write - the ASSEMBLER, the LINKER, the
  24.              LOADER and the 8086.
  25.  
  26.              1) The ASSEMBLER takes your text and turns it into the machine
  27.              code that is used by the 8086. It is complete except that the
  28.              addresses of data and subroutines might change during linking and
  29.              loading. The assembler generates information called HEADER files
  30.              which give the LINKER and LOADER the information they need to
  31.              update these addresses in the machine code. This means that you
  32.              can move the code anywhere in memory.
  33.  
  34.              2) If your program is made up of more than one file, the LINKER
  35.              links them together. It then makes it ready for running. If there
  36.              is only one file, the linker makes it ready for running. It does
  37.              this by updating the addresses of anything it has moved. It still
  38.              leaves the HEADER files which contain the segment addresses.
  39.  
  40.              3) At run time, the LOADER, which is part of the operating
  41.              system, decides where to put your program in memory. It loads the
  42.              program, and adjusts any segment addresses in the program to
  43.              reflect where the program actually is in memory. It then gives
  44.              control to the program.
  45.  
  46.              4) The code is fixed at the time the 8086 takes over. Any
  47.              addresses are constants and are unchangable. 
  48.  
  49.              Keep this in mind as we work through the template file.
  50.  
  51.  
  52.              THE .LST FILE
  53.  
  54.              The first thing we need to look at is segments. Let's look at a
  55.              slightly modified version of the template file called segs.asm.
  56.              Here it is. 
  57.  
  58.              ;*********************************** 
  59.              ; segs.asm 
  60.  
  61.              ______________________
  62.  
  63.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  64.  
  65.  
  66.  
  67.  
  68.              Chapter 10 - Templates                                         79
  69.              ______________________
  70.  
  71.  
  72.              ; - - - - - - - - - - - - - 
  73.              STACKSEG SEGMENT STACK  'STACK' 
  74.               
  75.              variable4    dw     4444h 
  76.                           dw     100h dup (?) 
  77.               
  78.              STACKSEG  ENDS 
  79.              ; - - - - - - - - - - - - - 
  80.              MORESTUFF SEGMENT PUBLIC  'HOHUM' 
  81.               
  82.              variable2    dw   2222h 
  83.               
  84.              MORESTUFF    ENDS 
  85.              ; - - - - - - - - - - - - -  
  86.              DATASTUFF  SEGMENT PUBLIC  'DATA' 
  87.               
  88.              variable1    dw     1111h 
  89.               
  90.              DATASTUFF    ENDS 
  91.              ; - - - - - - - - - - - - - 
  92.              CODESTUFF SEGMENT PUBLIC  'CODE' 
  93.               
  94.               EXTRN  print_num:NEAR , get_num:NEAR  
  95.               
  96.               ASSUME cs:CODESTUFF,ds:DATASTUFF 
  97.               ASSUME es:MORESTUFF,ss:STACKSEG 
  98.               
  99.              variable3    dw     3333h 
  100.               
  101.              main   proc far 
  102.              start: push  ds    
  103.                     sub   ax,ax 
  104.                     push  ax 
  105.               
  106.                     mov   ax, DATASTUFF 
  107.                     mov   ds,ax 
  108.                     mov   ax, MORESTUFF 
  109.                     mov   es,ax 
  110.               
  111.                     mov   cx, variable1 
  112.                     mov   variable1, cx 
  113.               
  114.                     ret 
  115.               
  116.              main   endp 
  117.               
  118.               
  119.              CODESTUFF    ENDS 
  120.              ; - - - - - - - - - - - -  
  121.               
  122.                  END     start 
  123.              ;***************************
  124.  
  125.              There is an extra segment put in that has the definition
  126.  
  127.                  MORESTUFF SEGMENT PUBLIC  'HOHUM' 
  128.  
  129.  
  130.  
  131.  
  132.              The PC Assembler Tutor                                         80
  133.              ______________________
  134.  
  135.  
  136.              There is a variable defined in each segment including the stack
  137.              segment. These variables all have numbers in them, and the
  138.              numbers are in hex so they will be easy to read. There are only
  139.              two external subroutines (neither of which is called). It is time
  140.              to take a look at an assembler listing. 
  141.  
  142.              ----- THIS IS FROM THE SCREEN -----
  143.  
  144.              C>masm segs
  145.              Microsoft (R) Macro Assembler Version 5.10
  146.              Copyright (C) Microsoft Corp 1981, 1988.  All rights reserved.   
  147.                            
  148.              Object filename [segs.OBJ]:               
  149.              Source listing  [NUL.LST]: segs
  150.              Cross-reference [NUL.CRF]:         
  151.  
  152.              -----------                               
  153.  
  154.              If you don't put a semicolon after the filename with masm, you
  155.              get some prompts. The first asks you if you want the object file
  156.              name to be different from the asm file name. You may change
  157.              either the name or the name and the extension. If you don't want
  158.              to change either, just press ENTER. The second asks if you want a
  159.              listing. Normally you don't, so you just press ENTER. This time
  160.              we do, so we give it the same name as the assembler file. The
  161.              assembler will generate a file SEGS.LST. Finally, it asks if you
  162.              want the information needed to create a cross-reference file. We
  163.              won't cover that. Once again, press ENTER. The assembler
  164.              generates an object file and a listing. Here's the complete
  165.              listing.
  166.  
  167.              **********************
  168.              Microsoft (R) Macro Assembler Version 5.10                 
  169.              9/2/89 09:50:54
  170.  
  171.                                                        Page     1-1 
  172.               
  173.               
  174.                                  
  175.                                 ; segs.asm 
  176.                                  
  177.                                 ; - - - - - - - - - - - - - 
  178.               0000                   STACKSEG SEGMENT STACK  'STACK' 
  179.                                  
  180.               0000  4444             variable4    dw     4444h 
  181.               0002  0100[                         dw     100h dup (?) 
  182.                     ????              
  183.                             ]    
  184.                                  
  185.                                  
  186.               0202                   STACKSEG  ENDS 
  187.                                 ; - - - - - - - - - - - - - 
  188.               0000                   MORESTUFF SEGMENT PUBLIC  'HOHUM' 
  189.                                  
  190.               0000  2222             variable2    dw   2222h 
  191.                                  
  192.  
  193.  
  194.  
  195.  
  196.              Chapter 10 - Templates                                         81
  197.              ______________________
  198.  
  199.               0002                   MORESTUFF    ENDS 
  200.                                 ; - - - - - - - - - - - - -  
  201.               0000                   DATASTUFF  SEGMENT PUBLIC  'DATA' 
  202.                                  
  203.               0000  1111             variable1    dw     1111h 
  204.                                  
  205.               0002                   DATASTUFF    ENDS 
  206.                                 ; - - - - - - - - - - - - - 
  207.               0000                   CODESTUFF SEGMENT PUBLIC  'CODE' 
  208.                                  
  209.                                  EXTRN  print_num:NEAR , get_num:NEAR  
  210.                                  
  211.                                  ASSUME cs:CODESTUFF,ds:DATASTUFF 
  212.                                  ASSUME es:MORESTUFF,ss:STACKSEG 
  213.                                  
  214.               0000  3333             variable3    dw     3333h 
  215.                                  
  216.               0002                   main   proc far 
  217.               0002  1E          start: push  ds    
  218.               0003  2B C0                   sub   ax,ax 
  219.               0005  50                 push  ax 
  220.                                  
  221.               0006  B8 ---- R               mov   ax, DATASTUFF 
  222.               0009  8E D8                   mov   ds,ax 
  223.               000B  B8 ---- R               mov   ax, MORESTUFF 
  224.               000E  8E C0                   mov   es,ax 
  225.                                  
  226.               0010  8B 0E 0000 R            mov   cx, variable1 
  227.               0014  89 0E 0000 R            mov   variable1, cx 
  228.                                  
  229.               0018  CB                 ret 
  230.                                  
  231.               0019                   main   endp 
  232.                                  
  233.                                  
  234.               0019                   CODESTUFF    ENDS 
  235.  
  236.              Microsoft (R) Macro Assembler Version 5.10                 
  237.              9/2/89 09:50:54
  238.               
  239.                                                        Page     1-2 
  240.               
  241.               
  242.                                 ; - - - - - - - - - - - -  
  243.                                  
  244.                                     END     start 
  245.  
  246.  
  247.              Microsoft (R) Macro Assembler Version 5.10                 
  248.              9/2/89 09:50:54
  249.               
  250.                                                                          
  251.              Symbols-1 
  252.               
  253.               
  254.              Segments and Groups: 
  255.               
  256.  
  257.  
  258.  
  259.  
  260.              The PC Assembler Tutor                                         82
  261.              ______________________
  262.  
  263.                        N a m e               Length Align Combine  Class 
  264.               
  265.              CODESTUFF  . . . . . . . . . . .  0019 PARA PUBLIC    'CODE' 
  266.              DATASTUFF  . . . . . . . . . . .  0002 PARA PUBLIC    'DATA' 
  267.              MORESTUFF  . . . . . . . . . . .  0002 PARA PUBLIC    'HOHUM' 
  268.              STACKSEG . . . . . . . . . . . .  0202 PARA STACK     'STACK' 
  269.               
  270.              Symbols:             
  271.               
  272.                      N a m e         Type     Value  Attr 
  273.               
  274.              GET_NUM  .  . . . .     L NEAR    0000 CODESTUFF External 
  275.               
  276.              MAIN . . .   . . . . .  F PROC    0002 CODESTUFF Length = 0017 
  277.               
  278.              PRINT_NUM   . . . .     L NEAR    0000 CODESTUFF External 
  279.               
  280.              START  . . . . . . . .  L NEAR    0002 CODESTUFF 
  281.               
  282.              VARIABLE1  . . . . . .  L WORD    0000 DATASTUFF 
  283.              VARIABLE2  . . . . . .  L WORD    0000 MORESTUFF 
  284.              VARIABLE3  . . . . . .  L WORD    0000 CODESTUFF 
  285.              VARIABLE4  . . . . . .  L WORD    0000 STACKSEG 
  286.               
  287.              @CPU . . . . . . . . . . . . . .  TEXT  0101h          
  288.              @FILENAME  . . . . . . . . . . .  TEXT  segs           
  289.              @VERSION . . . . . . . . . . . .  TEXT  510       
  290.               
  291.               
  292.                   54 Source  Lines 
  293.                   54 Total   Lines 
  294.                   21 Symbols 
  295.               
  296.                48006 + 428261 Bytes symbol space free 
  297.               
  298.                    0 Warning Errors 
  299.                    0 Severe  Errors 
  300.  
  301.              **********************
  302.  
  303.              As you can see, the listing, even for a short program, is very
  304.              long. Let's take it apart section by section. The first large
  305.              section is a copy of the text file except that there is
  306.              information on the left. The number on the far left tells the
  307.              offset address (in hex) from the beginning of the segment for
  308.              each label, variable or instruction. In this section:
  309.  
  310.                                  
  311.               0000  3333             variable3    dw     3333h 
  312.                                  
  313.               0002                   main   proc far 
  314.               0002  1E          start: push  ds    
  315.               0003  2B C0                   sub   ax,ax 
  316.               0005  50                 push  ax 
  317.                                  
  318.               0006  B8 ---- R               mov   ax, DATASTUFF 
  319.               0009  8E D8                   mov   ds,ax 
  320.  
  321.  
  322.  
  323.  
  324.              Chapter 10 - Templates                                         83
  325.              ______________________
  326.  
  327.               000B  B8 ---- R               mov   ax, MORESTUFF 
  328.               000E  8E C0                   mov   es,ax 
  329.                                  
  330.               0010  8B 0E 0000 R            mov   cx, variable1 
  331.               0014  89 0E 0000 R            mov   variable1, cx 
  332.                                  
  333.               0018  CB                 ret 
  334.                                  
  335.               0019                   main   endp 
  336.  
  337.              "start" is at 0002h ,"mov cx, variable1" is at 0010h and "ret" is
  338.              at 18h.
  339.  
  340.              The second set of numbers is the actual machine instructions in
  341.              hex. These are the what the 8086 operates on. "push ds" is 1E,
  342.              "mov ds, ax" is 8E D8, and "ret" is CB. The instructions can be
  343.              from 1 - 6 bytes long. Notice the "R" after some of the
  344.              instructions. The "R" stands for relocatable. This means that it
  345.              is an address that might be changed by either the linker or the
  346.              loader. We'll talk about that later. In any case, the object file
  347.              keeps track of these so they can be changed if necessary. Also,
  348.              go back to the complete listing and look at the four variables;
  349.              you will see that the values have been put in the object code;
  350.              that is, 1111h, 2222h, 3333h and 4444h.
  351.  
  352.              If we had had an error, the assembler would have placed an error
  353.              message at the spot of the error in this part of the file. 
  354.  
  355.  
  356.              The next part of the .LST file is the segment listing. It tells
  357.              how the segments are defined.
  358.  
  359.                      N a m e                 Length Align Combine  Class 
  360.               
  361.                  CODESTUFF  . . . . . . . .    0019 PARA PUBLIC    'CODE' 
  362.                  DATASTUFF  . . . . . . . .    0002 PARA PUBLIC    'DATA' 
  363.                  MORESTUFF  . . . . . . . .    0002 PARA PUBLIC    'HOHUM' 
  364.                  STACKSEG . . . . . . . . .    0202 PARA STACK     'STACK' 
  365.               
  366.  
  367.              We have the segment name, length, and some other information
  368.              we'll talk about later. Notice that 'HOHUM' which is an
  369.              artificial class, is dutifully listed with no complaints.
  370.  
  371.  
  372.              Then comes the listing of all labels, variables, and procedure
  373.              names.
  374.  
  375.                  Symbols:             
  376.               
  377.                      N a m e         Type     Value  Attr 
  378.               
  379.                  GET_NUM  . . . .    L NEAR    0000 CODESTUFF External  
  380.                  MAIN   . . . . .    F PROC    0002 CODESTUFF Length = 0017  
  381.                  PRINT_NUM    . .    L NEAR    0000 CODESTUFF External  
  382.                  START  . . . . .    L NEAR    0002 CODESTUFF  
  383.                  VARIABLE1  . . .    L WORD    0000 DATASTUFF 
  384.  
  385.  
  386.  
  387.  
  388.              The PC Assembler Tutor                                         84
  389.              ______________________
  390.  
  391.                  VARIABLE2  . . .    L WORD    0000 MORESTUFF 
  392.                  VARIABLE3  . . .    L WORD    0000 CODESTUFF 
  393.                  VARIABLE4  . . .    L WORD    0000 STACKSEG 
  394.  
  395.  
  396.              It shows the segment and offset, whether they are bytes, words,
  397.              processes etc. The "L" stands for label. The variables and
  398.              procedures which are in an external file are so marked. Neither
  399.              print_num nor get_num was called, but the assembler maintains a
  400.              listing for them.
  401.  
  402.              Finally, some internal info for the assembler.
  403.  
  404.              @CPU . . . . . . . . . . . . . .  TEXT  0101h          
  405.              @FILENAME  . . . . . . . . . . .  TEXT  segs           
  406.              @VERSION . . . . . . . . . . . .  TEXT  510       
  407.  
  408.              We will come back to parts of the .LST file, so make yourself
  409.              comfortable with it. 
  410.  
  411.  
  412.  
  413.              SEGMENTS
  414.  
  415.              It is now time for the nitty-gritty. We need to know what all
  416.              those statements in the template file mean. Remember that there
  417.              are four players in the game - (1) MASM, the Microsoft assembler,
  418.              (2) LINK, the Microsoft linker, (3) the program loader and (4)
  419.              the 8086 chip itself. Who does what to whom is the subject of
  420.              this chapter.
  421.  
  422.              You will notice that there are three segments in all the template
  423.              files, one for data, one for code, and one for the stack. How
  424.              many segments can a program have? An unlimited number for code,
  425.              an unlimited number for data, and one for the stack.{1}  Although
  426.              you can have an unlimited number of segments, you can use only
  427.              four at any one time - two for regular data (referenced by the DS
  428.              and ES registers), one for code (referenced by the CS register),
  429.              and one for temporary data (referenced by the SS register).
  430.  
  431.              You don't have direct control over CS. You should NEVER change
  432.              the value in SS. This means that you can only change which
  433.              segments that ES and DS refer to. How do you do that? The 8086
  434.              does not allow you to move a constant into a segment register.
  435.              Therefore it is a two step process. Put the constant into an
  436.              arithmetic register (AX, BX, CX, DX, SI, DI or BP) and from there
  437.              to the segment register. Suppose we have 327 different data
  438.              segments in our file (named SEG1, SEG2, SEG3 ... SEG327) and we
  439.              wanted to reference data in SEG27. The code would be:
  440.  
  441.                  mov  ax, SEG27
  442.                  mov  ds, ax
  443.  
  444.              ____________________
  445.  
  446.                 1 Although if you REALLY need more space for a stack it is
  447.              possible, if a little arcane.
  448.  
  449.  
  450.  
  451.  
  452.              Chapter 10 - Templates                                         85
  453.              ______________________
  454.  
  455.              This is the standard way to do it, and this is the same as the
  456.              fourth and fifth instructions in the code segment of the template
  457.              files where we are putting the address of DATASTUFF in ds.
  458.  
  459.              What is that SEG27 in the instruction (mov  ax, SEG27)? It is a
  460.              constant. When the assembler assembles the program, it makes note
  461.              of the fact that you want to have the starting address of SEG27
  462.              in that instruction (you saw the "R" in the listing for the
  463.              instruction 'mov ax, DATASTUFF'). Later the linker makes sure
  464.              there is a SEG27 segment in the complete program, gives it a
  465.              temporary segment address, and puts this temporary address in
  466.              every place that references that segment address. This address is
  467.              guaranteed to be adjusted. You will see why when we look at the
  468.              linker .MAP file.
  469.  
  470.              Finally, the loader (which is the program that puts your program
  471.              into memory) puts the segment where it wants and updates all
  472.              references to the segment address to reflect where it now is.
  473.              Thus, the program is complete only when this information is put
  474.              in at run time. Each time you run the program SEG27 might be in a
  475.              different place, but the loader will always update the references
  476.              correctly. 
  477.  
  478.              We named the segments SEG1, SEG2, etc. Does SEG have to be part
  479.              of the segment name? Not on your life. Here are three perfectly
  480.              acceptable segment definitions:
  481.  
  482.                  CURLY     SEGMENT
  483.                  LARRY     SEGMENT
  484.                  MOE       SEGMENT
  485.  
  486.              It is good practice to have 'SEG' as part of the segment name to
  487.              remind you that these are segments, not variables, but this is a
  488.              practice only, it is not a law. Any name you could use for a
  489.              variable or a label you could use as a segment name. The reserved
  490.              word SEGMENT after the name tells the assembler that this is the
  491.              beginning of a segment with that name. You tell the assembler
  492.              that you are starting a segment with 'SEGMENT'
  493.  
  494.                  CURLY     SEGMENT
  495.  
  496.              and you tell the assembler that you are finished with that
  497.              segment with the reserved word ENDS (END [of] Segment):
  498.  
  499.                  CURLY     ENDS
  500.  
  501.              You need to put the name of the segment before the  ENDS
  502.              directive.
  503.  
  504.              In the template file, the data segment definition reads:
  505.  
  506.                  DATASTUFF  SEGMENT  PUBLIC    'DATA'
  507.  
  508.              DATASTUFF is the segment name, but what are PUBLIC and 'DATA'
  509.              there for? To understand this, we need to look at the linker.
  510.              First, let's assemble temp1.asm (our first template file) just
  511.              the way it is. 
  512.  
  513.  
  514.  
  515.  
  516.              The PC Assembler Tutor                                         86
  517.              ______________________
  518.  
  519.  
  520.              ---------- FROM THE SCREEN ----------
  521.              C>masm temp1.asm          
  522.              Microsoft (R) Macro Assembler Version 5.10           
  523.              Copyright (C) Microsoft Corp 1981, 1988.  All rights reserved.   
  524.                   
  525.                      
  526.              Object filename [temp1.OBJ]:       
  527.              Source listing  [NUL.LST]: temp1            
  528.              Cross-reference [NUL.CRF]:         
  529.  
  530.              ----------
  531.              We have made the listing file so let's look at the segment
  532.              information.
  533.  
  534.  
  535.              N a m e                         Length Align Combine Class 
  536.               
  537.              CODESTUFF  . . . . . . . . . . .  000A PARA PUBLIC    'CODE' 
  538.              DATASTUFF  . . . . . . . . . . .  0000 PARA PUBLIC    'DATA' 
  539.              STACKSEG . . . . . . . . . . . .  00C8 PARA STACK     'STACK' 
  540.  
  541.              You will see that CODESTUFF is Ah (10d) bytes long, DATASTUFF has
  542.              no data and is 0 bytes long, and STACKSEG is C8h (200d) bytes
  543.              long.
  544.  
  545.              Now let's link temp1.obj and asmhelp.obj.
  546.  
  547.              ---------- FROM THE SCREEN -----
  548.  
  549.              C>link temp1+asmhelp      
  550.                      
  551.              Microsoft (R) Overlay Linker  Version 3.61           
  552.              Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.    
  553.                   
  554.                      
  555.              Run File [TEMP1.EXE]:              
  556.              List File [NUL.MAP]: temp          
  557.              Libraries [.LIB]:         
  558.  
  559.              ----------
  560.  
  561.              This time we have made a listing file for the link process. It is
  562.              called TEMP.MAP. Let's look at it.
  563.  
  564.                   Start  Stop   Length Name         Class 
  565.                   00000H 000C7H 000C8H STACKSEG     STACK 
  566.                   000D0H 00540H 00471H DATASTUFF    DATA 
  567.                   00550H 01944H 013F5H CODESTUFF    CODE 
  568.               
  569.                  Program entry point at 0055:0000 
  570.  
  571.              This is what the map file looks like. There are still only three
  572.              segments in the final executable file, STACKSEG, DATASTUFF and
  573.              CODESTUFF. You will notice that the class name is still there,
  574.              but the PUBLIC is missing. It's job is finished. "Start" says
  575.              where the segment starts in the executable file, "Stop" says
  576.  
  577.  
  578.  
  579.  
  580.              Chapter 10 - Templates                                         87
  581.              ______________________
  582.  
  583.              where the segment ends in the executable file, and "Length" says
  584.              the length in bytes of the segment. These numbers are 5 digit hex
  585.              numbers instead of 4. That means that they are showing the total
  586.              address. The segment number is the left 4 digits of 'Start'.
  587.  
  588.              STACKSEG is C8h (200d) bytes long like before. Although DATASTUFF
  589.              had no data, it is now 471h (1137d) bytes long, and CODESTUFF was
  590.              Ah (10d) bytes long before but now it is a whopping 13F5h (5109d)
  591.              bytes long. What happened? The linker did its work. 
  592.  
  593.              One of the things the linker does is combine things that we want
  594.              to be in the same segment. It took the DATASTUFF segment from
  595.              temp1.obj and appended the DATASTUFF segment from asmhelp.obj,
  596.              combining them into one larger segment.{2}  It took the CODESTUFF
  597.              segment from temp1.obj and appended the CODESTUFF segment from
  598.              asmhelp.obj, making them one large segment. Why did it do that?
  599.              Because we put the word "PUBLIC" in the segment definition. When
  600.              the assembler sees "PUBLIC" in the segment definition, it passes
  601.              that information along to the linker in a header file.{3}  When
  602.              the linker has a segment which is "PUBLIC", it will append any
  603.              other segment which (1) is "PUBLIC", (2) has the same name (i.e.
  604.              CODESTUFF or DATASTUFF or CURLY etc.), and (3) has the same class
  605.              name{4}. All three things must be true for the linker to combine
  606.              them. We will actually check this out a little later to make sure
  607.              you believe it.
  608.  
  609.              One other thing to notice is that the linker is allocating only
  610.              as much space as is needed. It could allocate 65536 bytes for
  611.              each segment defined, but it uses only as much as the program
  612.              needs and then starts the next segment at the next segment
  613.              starting address. This is efficient management of memory. 
  614.  
  615.              What is the advantage of combining the smaller segments into one
  616.              larger segment? For code, there is no big advantage. But for
  617.              data, remember that every time we want to access data, we need to
  618.              have the starting address of that particular segment in register
  619.              ds. We do this by using:
  620.  
  621.                  mov  ax, DATASTUFF
  622.                  mov  ds, ax
  623.  
  624.              If we have a number of data segments, every time we access data
  625.              ____________________
  626.  
  627.                 2 The linker always works from left to right. For each
  628.              different type of segment, it starts with the first one it finds
  629.              and then appends each succeeding one it finds.
  630.  
  631.                 3 A header is information for the linker or loader which is
  632.              put in front of the machine code in an object file or an
  633.              executable file. There are typically a number of headers in front
  634.              of the machine code.
  635.  
  636.                 4 Remember that class names are somewhat arbitrary. I use
  637.              'CODE', 'DATA' and 'STACK' for clarity and because they are the
  638.              standard Microsoft class names, but if you are not linking with
  639.              anyone else's programs, you can use any class name you want.
  640.  
  641.  
  642.  
  643.  
  644.              The PC Assembler Tutor                                         88
  645.              ______________________
  646.  
  647.              we need to (1) make sure that ds contains the address of the
  648.              correct data segment, and (2) if not, we need to write the code
  649.              to change ds. This entails using a lot of code, can be confusing
  650.              and is certainly error prone. With one data segment, you simply
  651.              load ds with the correct address at the beginning of the program
  652.              and then forget about it. This should be a rule for you. Unless
  653.              you have truly humongous amounts of data (over 65535 bytes),
  654.              ALWAYS put all your data in the same segment. 
  655.  
  656.              Do you remember those dashes '----' in the assembler listing?
  657.              That was because the assembler didn't have a segment address to
  658.              put there.
  659.  
  660.                   0004  B8 ---- R                mov   ax, DATASTUFF 
  661.                   0007  8E D8                    mov   ds,ax 
  662.                                  
  663.                   0009  8B 0E 0000 R             mov   cx, variable1 
  664.                   000D  89 0E 0000 R             mov   variable1, cx 
  665.  
  666.              The linker now has a temporary address for the start of DATASTUFF
  667.              (000D0h) so it will put the segment address (the left four hex
  668.              bytes) in this spot. This is temporary, but will be updated by
  669.              the loader. If variable1 has been moved, it will update that too.
  670.  
  671.              Why am I sure that these temporary segments will be moved? The
  672.              segment address of STACKSEG is 0000h. The segment address of
  673.              DATASTUFF is 000Dh (13h) and the segment address of CODESTUFF is
  674.              0055h (85d). But the operating sysyem owns the first several
  675.              THOUSAND segments. The loader will load your program in much
  676.              higher memory. They must move.
  677.  
  678.              So the linker combines all the segments we want to combine, and
  679.              then it looks at the machine code and modifies every reference to
  680.              the segments and to the variables which have been moved. That is
  681.              a lot of work. For instance, when the linker appends asmhelp.obj,
  682.              there are a hundred or so variables which it moves and a thousand
  683.              or so references to those variables which it modifies. The linker
  684.              does that every time you link a file with ASMHELP.OBJ. That's not
  685.              too shabby.
  686.  
  687.